/*
 * Copyright (C) 2023 KylinSoft Co., Ltd.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3, or (at your option)
 * any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, see <http://www.gnu.org/licenses/>.
 *
**/
#include "networkbutton.h"
#include <QStylePainter>
#include <QStyleOption>
#include <QMouseEvent>

NetWorkButton::NetWorkButton(QWidget *parent)
    : KyNetworkIcon(parent)
{
    setFixedSize(48, 48);
    setIconSize(QSize(24, 24));
    setFocusPolicy(Qt::NoFocus);
}

void NetWorkButton::setClickedStatus(int status)
{
    curStatus = status;
    update();
}

void NetWorkButton::paintEvent(QPaintEvent *e)
{
    Q_UNUSED(e);
    QStylePainter painter(this);
    painter.setRenderHint(QPainter::Antialiasing);
    QStyleOptionButton option;
    initStyleOption(&option);

//    if (curStatus == NORMAL) {
//        painter.save();
//        painter.setPen(Qt::NoPen);
//        painter.setBrush(Qt::white);
//        painter.setOpacity(0);
//        painter.drawRoundedRect(option.rect.adjusted(1, 1, -1, -1), 6, 6);
//        painter.restore();
//    }

//    if (option.state & QStyle::State_MouseOver) {
//        painter.save();
//        painter.setPen(Qt::NoPen);
//        painter.setBrush(Qt::white);
//        painter.setOpacity(0.25);
//        painter.drawRoundedRect(option.rect.adjusted(1, 1, -1, -1), 6, 6);
//        painter.restore();
//    }

    if (curStatus == CLICKED) {
        painter.save();
        painter.setPen(Qt::NoPen);
        painter.setBrush(Qt::white);
        painter.setOpacity(0.35);
        painter.drawRoundedRect(option.rect.adjusted(1, 1, -1, -1), 6, 6);
        painter.restore();
    }

    if (curStatus == SELECT) {
        painter.save();
        painter.setPen(Qt::NoPen);
        painter.setBrush(Qt::white);
        painter.setOpacity(0.25);
        painter.drawRoundedRect(option.rect.adjusted(1, 1, -1, -1), 6, 6);
        painter.restore();

        painter.save();
        QStyleOption opt;
        QColor color = opt.palette.color(QPalette::Highlight);
        painter.setPen(QPen(color, 2));
        painter.setBrush(Qt::NoBrush);
        painter.drawRoundedRect(option.rect.adjusted(1, 1, -1, -1), 6, 6);
        painter.restore();
    }
    QPushButton::paintEvent(e);

}

void NetWorkButton::mouseReleaseEvent(QMouseEvent *e)
{
    e->ignore();
}

void NetWorkButton::mousePressEvent(QMouseEvent *e)
{
    e->ignore();
}
